package user

import (
	"context"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserIpStatLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserIpStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserIpStatLogic {
	return &UserIpStatLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserIpStatLogic) UserIpStat(req *types.UserIpStatRequest) (resp *types.UserIpStatResponse, err error) {
	rpcResp, err := l.svcCtx.UserServiceRPC.UserIpStat(l.ctx, &userrpc.UserIpStatRequest{
		Ip: req.Ip,
	})
	if err != nil {
		logc.Error(l.ctx, "UserIpStat rpc call error: %v", err)
		return &types.UserIpStatResponse{
			IpStat: []*types.IpStat{},
		}, nil
	}
	resp = &types.UserIpStatResponse{}
	ipStats := make([]*types.IpStat, len(rpcResp.Data))

	for i, item := range rpcResp.Data {
		ipStats[i] = &types.IpStat{
			Ip:    item.Ip,
			Count: item.Count,
		}

	}
	resp.IpStat = ipStats
	return
}
